// DlgSetAccount.cpp : 实现文件
//

#include "AIPanelDataIMSales.h"

#include <qbprotocol/include/SSMsgStruct.h>
#include <qbprotocol/include/rdProto.h>
#include <qbtools/message/MessagePoster.h>
#include <uam/UserAccountManager.h>

#include <QColor>
#include <QFontMetrics>
#include <QPainter>
#include <QStylePainter>

#include "../StringResource.h"
#include "DlgAccCheck.h"
#include "auth/MessageProcess.h"

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// class CAIPanelDataIMSales
CAIPanelDataIMSales::CAIPanelDataIMSales() {
  m_imgSuccess = ImgMgr::instance().image("guiframe_icon_tips_success");
  m_imgCaution = ImgMgr::instance().image("guiframe_icon_tips_caution");
  m_nStatus = 0;
  m_rcButton = QRect(0, 0, 0, 0);
  m_btnApply = NULL;
}

CAIPanelDataIMSales::~CAIPanelDataIMSales() {}

void CAIPanelDataIMSales::SetApplyStatus(int nStatus) { m_nStatus = nStatus; }

void CAIPanelDataIMSales::Init() {
  const int nFontHeight = 24;
  const int nGap = 8;

  m_nHeight = (nFontHeight + nGap) * 2;

  m_nStatus = 0;
  if (strcmp(SSUserAccountManager::instance().GetPermission().GetValue(
                 USER_PERMISSION_NGPC_SALES),
             "1") == 0)
    m_nStatus = 1;
  else {
    xQBUserpermApplyData data;
    if (SSUserAccountManager::instance().GetPermission().GetStatus(
            USER_PERMISSION_NGPC_SALES, data)) {
      switch (data.m_applyStatus) {
        case 1:
          m_nStatus = 2;
          break;
        case 2:
          m_nStatus = 1;
          break;
        case 3:
          m_nStatus = 0;
          break;
        default:
          break;
      }
    } else {
      xQBUserPermApplyReq_t stReq;
      FIELDCOPY(stReq.m_userId,
                SSUserAccountManager::instance().GetUserInfo().m_UserId);
      FIELDCOPY(stReq.m_functionCode, USER_PERMISSION_NGPC_SALES);

      qb::SSRequestMsg msg;
      msg.m_pRequest = &stReq;
      msg.m_FuncID = E_FID_QB_USERPERM_APPLY_QUERY;
      MessagePoster poster(&msg, auth::EncodeFunc);
      MessageReceiver* pmr = dynamic_cast<MessageReceiver*>(m_pParent);
      poster.send(pmr);
    }
  }
  if (m_nStatus == 0) {
    CreateButton();
    connect(m_btnApply, &QPushButton::clicked, this,
            &CAIPanelDataIMSales::OnBtnOk);
  }
}

void CAIPanelDataIMSales::Draw(QPainter* painter, int top) {
  m_rcButton = QRect(0, 0, 0, 0);

  QColor brHead = QColor(230, 179, 66);
  QColor brFront = NAME_COLOR("TextNormal");
  QColor brButton = NAME_COLOR("BorderNormal");

  QString strTemp =
      NewStringTable::strAIPanelDataIMSales(0);  // 数据权限 - 一级销售

  QRect r = rect();
  int nGap = 8;

  // 数据权限-CFETS
  int X = nGap;
  int Y = 0;
  QRect textRect = QRect(X, Y, r.right() - X, 24);
  painter->setPen(QPen(brHead));
  painter->setFont(m_lfHead);
  painter->drawText(textRect, strTemp,
                    QTextOption(Qt::AlignLeft | Qt::AlignVCenter));

  X += 24;
  Y += 24 + nGap;
  int nMaxWidth = 1000;
  int nImgHeight = 20;
  if (m_imgSuccess) nImgHeight = m_imgSuccess->height();

  painter->setFont(QFont(m_lfContent));
  switch (m_nStatus) {
    case 0: {
      // 无权限
      strTemp =
          NewStringTable::strAIPanelDataIMSales(3);  // 您尚未获得一级销售权限
      QFontMetrics fontMetrics(m_lfContent);
      int nFontWidth = fontMetrics.width(strTemp);

      textRect = QRect(X, Y, nMaxWidth, 24);
      painter->setPen(QPen(brFront));
      painter->drawText(textRect, strTemp,
                        QTextOption(Qt::AlignLeft | Qt::AlignVCenter));

      X += nFontWidth;
      strTemp = NewStringTable::strAIPanelDataIMSales(4);  // NOT
      fontMetrics = QFontMetrics(m_lfContent);
      nFontWidth = fontMetrics.width(strTemp);
      textRect = QRect(X, Y, nMaxWidth, 24);
      painter->setPen(QPen(brHead));
      painter->drawText(textRect, strTemp,
                        QTextOption(Qt::AlignLeft | Qt::AlignVCenter));

      X += nFontWidth;
      strTemp = NewStringTable::strAIPanelDataIMSales(
          5);  // been authorized to access Bond Sales data.

      textRect = QRect(X, Y, nMaxWidth, 24);
      painter->setPen(QPen(brFront));
      painter->drawText(textRect, strTemp,
                        QTextOption(Qt::AlignLeft | Qt::AlignVCenter));

      break;
    }
    case 1: {
      if (m_imgSuccess) {
        int x = X + (24 - nImgHeight) / 2;
        int y = Y + (24 - nImgHeight) / 2;
        QPixmap m_tipssuccess = QPixmap::fromImage(*m_imgSuccess);
        painter->drawPixmap(x, y, m_tipssuccess);
        X += 24 + 1;
      }
      strTemp =
          NewStringTable::strAIPanelDataIMSales(1);  // 您已获得一级销售权限。
      QRect textRect = QRect(X, Y, nMaxWidth, 24);
      painter->setPen(QPen(brFront));
      painter->drawText(textRect, strTemp,
                        QTextOption(Qt::AlignLeft | Qt::AlignVCenter));
      break;
    }
    case 2: {
      if (m_imgCaution) {
        nImgHeight = m_imgCaution->height();
        int x = X + (24 - nImgHeight) / 2;
        int y = Y + (24 - nImgHeight) / 2;
        QPixmap m_tipCaution = QPixmap::fromImage(*m_imgCaution);
        painter->drawPixmap(x, y, m_tipCaution);
        X += 24 + 1;
      }
      strTemp = NewStringTable::strAIPanelDataIMSales(2);  // 已申请，等待审核中

      QRect textRect = QRect(X, Y, nMaxWidth, 24);
      painter->setPen(QPen(NAME_COLOR("Highlight")));
      painter->drawText(textRect, strTemp,
                        QTextOption(Qt::AlignLeft | Qt::AlignVCenter));
      break;
    }
    case 3:
    default:
      break;
  }
}

void CAIPanelDataIMSales::resizeEvent(QResizeEvent* event) {
  int btnWidth = 80;
  int btnHeight = 24;
  int Y = 24 + 8;
  QRect r = rect();
  m_rcButton.setRight(r.right() - 24 - 8);
  m_rcButton.setLeft(m_rcButton.right() - btnWidth);
  m_rcButton.setTop(Y + (24 - btnHeight) / 2);
  m_rcButton.setBottom(m_rcButton.top() + btnHeight);

  if (m_btnApply != NULL) m_btnApply->move(m_rcButton.left(), m_rcButton.top());
}

void CAIPanelDataIMSales::paintEvent(QPaintEvent* event) {
  QStylePainter painter(this);
  Draw(&painter, 0);
}

void CAIPanelDataIMSales::OnBtnOk() {
  CDlgAccCheck* mobileCfmDlg = new CDlgAccCheck(this);
  mobileCfmDlg->show();

  xQBUserPermApplyReq_t m_Req;
  FIELDCOPY(m_Req.m_userId,
            SSUserAccountManager::instance().GetUserInfo().m_UserId);
  FIELDCOPY(m_Req.m_functionCode, USER_PERMISSION_NGPC_SALES);
  FIELDCOPY(m_Req.m_permValue, "1");

  qb::SSRequestMsg msg;
  msg.m_pRequest = &m_Req;
  msg.m_FuncID = E_FID_QB_SERVICE_PLATFORM_MANAGER_REQ;
  MessagePoster poster(&msg, auth::EncodeFunc);
  MessageReceiver* pmr = dynamic_cast<MessageReceiver*>(m_pParent);
  poster.send(pmr);
}

void CAIPanelDataIMSales::CreateButton() {
  m_btnApply = new QPushButton("立即申请", this);
  QString style =
      QString(
          "QPushButton{background:#%1; "
          "border-radius:0px;color:#FFE5C7}\nQPushButton:hover{background:#%2}"
          "\n")
          .arg(StyleMgr::instance().color("ButtonNormal").rgb(), 0, 16)
          .arg(StyleMgr::instance().color("Hover").rgb(), 0, 16);
  m_btnApply->setStyleSheet(style);
  m_btnApply->setFixedSize(80, 24);
}
